//
//  CouponDetailVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/1/24.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "CouponDetailVC.h"
#import <LBXScanNative.h>
#import "ScenicDescribeVC.h"
#import "LinePreDetailVC.h"
#import "IGShare.h"
@interface CouponDetailVC ()

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *describeLabel;

@property (weak, nonatomic) IBOutlet UIImageView *codeImageView;
@property (weak, nonatomic) IBOutlet UILabel *codeLabel;

@property (weak, nonatomic) IBOutlet UIButton *shareBtn;
@property (weak, nonatomic) IBOutlet UIButton *useBtn;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;
@property (weak, nonatomic) IBOutlet UILabel *couponInfoLabel;


@end

@implementation CouponDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)setup {
    
    
    self.couponInfoLabel.text = MYLocalizedString(@"VOUCHER_SHARE_INFO", nil);
    self.title = MYLocalizedString(@"PAYORDER_VOUCHER", nil);
    
    self.shareBtn.layer.borderWidth = 1.0f;
    self.shareBtn.layer.borderColor = kThemeColor.CGColor;
    self.useBtn.layer.borderWidth = 1.0f;
    self.useBtn.layer.borderColor = kThemeColor.CGColor;
    
    self.nameLabel.text = self.coupon.infos[@"title"];
    self.typeLabel.text = self.coupon.infos[@"middle"];
    self.priceLabel.text = self.coupon.infos[@"big"];
    self.describeLabel.text = self.coupon.infos[@"desc"];
    
    NSString *textString = [NSString stringWithFormat:@"%@:%@",MYLocalizedString(@"VOUCHER_CODE", nil),self.coupon.title];
    
    NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString];
    mutableAttributedString.font = Font(12);
    mutableAttributedString.color = kContentColor;
    mutableAttributedString.lineSpacing = 8;
    
    NSRange range = [textString rangeOfString:self.coupon.title];
    
    [mutableAttributedString setColor:kThemeColor range:range];
    
    self.codeLabel.attributedText = mutableAttributedString;
    self.codeLabel.userInteractionEnabled = YES;
    
    [self.codeLabel addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithActionBlock:^(UILongPressGestureRecognizer *sender) {
        
        if (sender.state != UIGestureRecognizerStateEnded
            && sender.state != UIGestureRecognizerStateChanged) {
            
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = self.coupon.title;
            
            [IGUtil showToast:MYLocalizedString(@"CONTACT_COPY_SUCCESS", nil)];
        }
        
    }]];
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    [self createQRImage];
}

- (void)createQRImage {
    
    NSDictionary *dict = @{@"type":@"coupon", @"code":self.coupon.title ? self.coupon.title : @""};
    
    NSString *json = [dict modelToJSONString];
    
    UIImage *image = [LBXScanNative createQRWithString:json QRSize:self.codeImageView.bounds.size];
    
    self.codeImageView.image = image;
    [self.activityView stopAnimating];
    
}

- (IBAction)shareAction:(id)sender {
    
    [self popSheet];
}
- (IBAction)useAction:(id)sender {
    
    if ([self.coupon.infos[@"gototype"] isEqualToString:@"view"]) { // 景点
        
        NSString *viewid = self.coupon.infos[@"gotoid"];
        
        ScenicDescribeVC *scenicVC = [ScenicDescribeVC new];
        scenicVC.scenicid = viewid;
        
        [self.navigationController pushViewController:scenicVC animated:YES];
        
    } else if ([self.coupon.infos[@"gototype"] isEqualToString:@"line"]) {//讲解
        
        [ActivityIndicatorView showHUDAddedTo:self.view];
        @weakify(self);
        [NetAPIManager useCouponid:self.coupon.couponid Block:^(NSString *lineid, NSString *errmsg) {
            
            @strongify(self);
            [ActivityIndicatorView hideHUDAddedTo:self.view];
            
            if (!errmsg) {
                
                
                LinePreDetailVC *detailVC = [IGUtil loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
                detailVC.lineid = lineid;
                
                [self.navigationController pushViewController:detailVC animated:YES];
            } else {
                
                [IGUtil showToast:errmsg];
            }
            
        }];
    }
}


#pragma mark - SnailSheetViewDelegate

- (void)sheetView:(SnailSheetView *)sheetView didSelectItemAtSection:(NSInteger)section index:(NSInteger)index {
    @weakify(self)
    SnailSheetItemModel *model = [self sheetModels][section][index];
    
    NSString * range = @"tourist/services/";
    NSString * str = [[AppManager sharedManager].baseURLStr substringToIndex:[AppManager sharedManager].baseURLStr.length -range.length];
    
    NSString * url = [NSString stringWithFormat:@"%@%@%@",str,@"shareh5/#/freeticket?id=",self.coupon.couponid];
    
    self.sl_popupController.didDismiss = ^(SnailPopupController * _Nonnull popupController) {
        @strongify(self);
        
        [IGShare shareWebPageToPlatformType:model.platformType withURL:url title:@"ImGuider 优惠券" descr:self.coupon.infos[@"desc"] thumImage:self.codeImageView.image object:nil type:2 completion:nil];
    };
    [self.sl_popupController dismiss];
}
@end

